package com.toggery.POS.controller;

import com.toggery.POS.entity.Clothing;
import com.toggery.POS.entity.Orderdetail;
import com.toggery.POS.entity.Orders;
import com.toggery.POS.service.ClothingService;
import com.toggery.POS.service.OrderDetailService;
import com.toggery.POS.service.OrderService;
import com.toggery.POS.tool.DateTool;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/order")
@Api(tags = "订单数据")
public class OrderController {
    @Autowired
    OrderService service;
    @Autowired
    ClothingService clothingService;
    @Autowired
    OrderDetailService detailService;
    @GetMapping("/list")
    public Map findAll(){
        Map map = new HashMap() ;
        map.put("data",service.findAll());
        return map;
    }

    @GetMapping("/listDetail/{id}")
    public Map findAllDetailWithId(@PathVariable int id){
        Map map = new HashMap() ;
        map.put("data",detailService.findAllWithOrderId(id));
        return map;
    }
    @PostMapping(value = "/delete/{id}")
    public Map deleteById(@PathVariable int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    public Map update(@RequestBody(required=false) Map map1){
        Orders order = service.findById(Integer.parseInt(map1.get("id").toString()));
//        order.setPrice(Double.parseDouble(map1.get("price").toString()));
        HashMap map = new HashMap();
        if(service.updateOrder(order)){
            map.put("result",true);
            map.put("result","更新成功");
        }else {
            map.put("result",false);
            map.put("result","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map save(@RequestBody(required=false) Map map1){
        Orders order = new Orders();
        order.setDescription(map1.get("description").toString());
        order.setOrdertype("未支付");
        order.setOrdertime(DateTool.getNow());
        order.setSaler("售货员");
        order.setSalerid("123");
        HashMap map = new HashMap();
        if(service.save(order)){
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }
    @PostMapping("/saveDetail")
    public Map saveDetail(@RequestBody(required=false) Map map1){
        int orderId = Integer.parseInt(map1.get("orderId").toString());
        Orders order ;
        Orderdetail orderdetail = new Orderdetail();
        orderdetail.setNumber(Integer.parseInt(map1.get("number").toString()));
        Clothing clothing = clothingService.findById(Integer.parseInt(map1.get("clothingId").toString()));
        orderdetail.setClothing(clothing.getClothingname());
        orderdetail.setClothingid(clothing.getId());
        orderdetail.setPrice(clothing.getPrice());
        orderdetail.setOrderid(orderId);
        double sum = clothing.getPrice()*Integer.parseInt(map1.get("number").toString());
        orderdetail.setSumprice(sum);
        HashMap map = new HashMap();
        if(detailService.save(orderdetail)){
            order = service.findById(orderId);
            if(order.getSumamount()==null){
                order.setSumamount(0+sum);
            }else {
                order.setSumamount(order.getSumamount()+sum);
            }
            service.updateOrder(order);
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }
}
